package org.walkerljl.practice.asyncparallel;

import org.walkerljl.practice.Task;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author lijunlin
 */
public class CompletionServiceTest2 {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(executorService);
        int taskAmount = 10;
        List<Future> futures = new ArrayList<Future>();
        for (int i = taskAmount; i > 0; i--) {
            futures.add(completionService.submit(new Task((long)i, i * 1)));
        }

        for (Future future : futures) {
            try {
                System.out.println(future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}
